import Packet from "../net/Packet"

// 请求登录
export default class RoleLogin {
	/**
	 * @param account 账号
	 * @param password 密码
	 * @param remark 备注
	 * @param video 语音
	 */
	public constructor(public account: string, public password: string, public remark?: string, public video?: Uint8Array) { }

	public encode(): Packet {
		const packet = this._encode()
		packet.Encode(1010)
		return packet
	}

	public encodeMsg(): ByteBuffer {
		return this._encode().GetBuffer()
	}

	public static decode(packet: Packet): RoleLogin {
		const account = packet.ReadString()
		const password = packet.ReadString()
		const remarkFlag = packet.ReadByte()
		let remark: string
		if (remarkFlag == 1) {
			remark = packet.ReadString()
		}
		const videoFlag = packet.ReadByte()
		let video: Uint8Array
		if (videoFlag == 1) {
			video = packet.ReadBin()
		}
		return new RoleLogin(account, password, remark, video)
	}

	private _encode(): Packet {
		const packet = new Packet()
		packet.WriteString(this.account)
		packet.WriteString(this.password)
		if (this.remark) {
			packet.WriteByte(1)
			packet.WriteString(this.remark)
		} else {
			packet.WriteByte(0)
		}
		if (this.video) {
			packet.WriteByte(1)
			packet.WriteBin(this.video)
		} else {
			packet.WriteByte(0)
		}
		return packet
	}
}
